package com.lijing.hashTab;

public class HashTabDemo {
    public static void main(String[] args) {
//        EmpLinkedList empLinkedList = new EmpLinkedList();
//        empLinkedList.add(new Emp(1,"lijing"));
//        empLinkedList.add(new Emp(2,"dingsuzhen"));
//        empLinkedList.del(1);
//        empLinkedList.find(2);
//        empLinkedList.list();
        HashTab hashTab = new HashTab(6);
        hashTab.add(new Emp(1,"lijing"));
        hashTab.add(new Emp(11,"dingsuzhen"));
        hashTab.add(new Emp(12,"zhangsan"));
        hashTab.list();
        hashTab.delById(12);
        hashTab.list();
    }
}

//hashtable
class HashTab{
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    public HashTab(int size) {
        this.size = size;
        //初始化empLinkedListArray
        empLinkedListArray = new EmpLinkedList[size];

        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }


    }

    //添加元素add
    public void add(Emp emp){
        //根据员工的id添加到指定的链表位置
        int empLinkedListNO = hashFun(emp.id);
        empLinkedListArray[empLinkedListNO].add(emp);
    }

    //遍历所有的HashTable
    public void list(){
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list(i);
        }
    }

    //根据id查找雇员
    public Emp findById(int id){
        return empLinkedListArray[hashFun(id)].find(id);
    }

    //根据id删除雇员
    public boolean delById(int id){
        return empLinkedListArray[hashFun(id)].del(id);
    }

    //编写散列函数
    public int hashFun(int id){
        return id%size;
    }
}

//represent a employer
class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
//
class EmpLinkedList {
    private Emp head;//default null

    public EmpLinkedList() {
        head = null;
    }

    /**
     * add a Emp
     * @param emp to be added
     */
    public void add (Emp emp){
        //add first para
        if (head == null){
            head = emp;
            return;
        }
        Emp temp = head;
        while (temp.next != null){
            temp = temp.next;
        }
        temp.next = emp;
    }

    /**
     * list all members
     */
    public void list(int no){
        if (head == null) {
            System.out.println("第"+ no +"条链表为空！");
            return;
        }
        System.out.println("第"+ no +"条链表的信息为:");
        Emp temp = head;
        while (temp != null){
            System.out.println(temp);
            temp = temp.next;
        }
    }

    /**
     * find the Emp where id == id
     * @param id id
     * @return  an emp
     */
    public Emp find(int id){
        if (head == null) {
            System.out.println("查找链表为空！");
            return null;
        }
        Emp temp = head;
        while(true){
            if(temp == null){
                System.out.println("There is no Emp where id is "+ id);
                return null;
            }
            if(id == temp.id){
                System.out.println("find the Emp:"+ temp);
                return temp;
            }
            temp = temp.next;
        }
    }

    /**
     * 删除指定id的元素
     * @param id id
     * @return 删除成功返回true，否则false
     */
    public boolean del(int id){
        if (head == null) {
            System.out.println("The list is empty! del " + id + " failed!");
            return false;
        }
        Emp temp = head;
        //要删除的是第一个元素
        if(head.id == id){
            head = temp.next;
            System.out.println("del " + id + " success!");
            return true;
        }
        while(true){
            if(temp.next == null){
                System.out.println("没有要删除的元素！");
                return false;
            }
            //遍历到要删除的前一个元素，才能进行删除
            if(temp.next.id == id){
                temp.next = temp.next.next;
                return true;
            }
            temp = temp.next;
        }


    }
}

